﻿/* File Created: 七月 15, 2012 */
$(function () {
    //窗口样式
    $(".login-text").focus(function () {
        $(this).addClass("login-text-focus");
    }).blur(function () {
        $(this).removeClass("login-text-focus");
    });
    //响应回车事件
    $(document).keyup(function (e) {
        if (e.keyCode === 13) {
            Login();
        }
    });
    $("#btnLogin").click(function () {
        Login();
    });

    //更换验证码
    //$("#imgVerifCode").attr("src", "Hander/VerificationCode.ashx");
    $("#txtPassword").blur(function () {
        $("#imgVerifCode").attr("src", "Hander/VerificationCode.ashx?=" + new Date());
    });
    $("#imgVerifCode").click(function () {
        $(this).attr("src", "Hander/VerificationCode.ashx?=" + new Date());
    });
    //登陆验证
    function Login() {
        var txtUserName = $("#txtUsername").val();
        var txtPwd = $("#txtPassword").val();
        var txtVerifCode = $("#txtVerifCode").val();
        if (txtUserName === "") {
            alert("账号不能为空!");
            $("#txtUsername").focus();
            return;
        }
        if (txtPwd === "") {
            alert("密码不能为空!");
            $("#txtPassword").focus();
            return;
        }
        if (txtVerifCode === "") {
            alert("验证码不能为空!");
            $("#imgVerifCode").focus();
            return;
        }
        var setting = { UserName: txtUserName, Pwd: txtPwd, VerifCode: txtVerifCode, Cookie: $("#ddlcookieday").val() };
        $.ajax({
            url: "Hander/LoginHandler.ashx?action=Login",
            type: "post",
            cache: false,
            data: { "data": JSON.stringify(setting) },
            dataType: "json",
            timeout: "5000",
            success: function (msg, state) {
                if (state === "success" && msg.IsSuccess !== true) {
                    alert(msg.Msg);
                    $("#txtUsername").focus();
                } else {
                    window.location = decodeURIComponent("index.aspx");
                }
            },
            error: function (msg) {
                $(".tbox").hide();
                alert("发送系统错误,请与系统管理员联系!");
            },
            beforeSend: function () {
                TINY.box.show({ html: "正在加载数据,请稍后...", width: "180", animate: false, close: false, mask: false, boxid: "success", autohide: 3, top: -12, left: -17 })
                $("#btnLogin").attr("disabled", true);
            },
            complete: function () {
                $(".tbox").hide();
                $("#btnLogin").attr("disabled", false);
            }
        });
    }
});